package cn.yunhe.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import cn.yunhe.biz.BookBiz;
import cn.yunhe.bizImpl.BookBizImpl;
import cn.yunhe.entity.Book;


public class BookAction extends BaseAction {
	private int bookId;
	private String bookName;
	private String bookAuthor;
	private String bookPublish;
	private String bookISBN;
	private double bookPrice;
	private int bookCurrCount;
	private String bookPicture;
	private String keyword;
	private String type;
	private File upload;
	private String uploadFileName;
	private String savePath;
	BookBiz bookBiz=new BookBizImpl();
	public String execute() throws Exception {
		List<Book> bookList=this.bookBiz.getBookByCondition(this.type, this.keyword);
		System.out.println(bookList.get(1).getBookPicture());
		getRequest().setAttribute("bookList", bookList);
		return SUCCESS;
	}
	public String getAllBook(){
		List<Book> bookList=this.bookBiz.getAllBook();
		getRequest().setAttribute("bookList", bookList);
		return SUCCESS;
	}
	public String queryOnlyBook(){
		Book book=bookBiz.getBookByBookId(bookId);
		getRequest().setAttribute("book", book);
		return SUCCESS;
	}
	public String deleteBookbyBookId(){
		boolean flag=this.bookBiz.deleteBookByBookId(this.bookId);
		if(flag){
			return SUCCESS;
		}else{
			return INPUT;
		}
	}
	public String updateBookByBookId(){
		System.out.println("jfdsk;fjsl;fjds;fjds;fjds;fjs");
		String uploadPath=getSavePath()+"\\"+getUploadFileName();
		doUpload(upload,uploadPath);
		Book book=new Book();
		book.setBookId(bookId);
		book.setBookAuthor(bookAuthor);
		book.setBookCurrCount(bookCurrCount);
		book.setBookISBN(bookISBN);
		book.setBookName(bookName);
		book.setBookPrice(bookPrice);
		book.setBookPublish(bookPublish);
		book.setBookPicture(uploadPath);
		boolean flag=bookBiz.updateBookByBookId(book);
		return SUCCESS;
		
	}
	public void doUpload(File upload,String uploadFilePath){
		try{
			FileOutputStream fos=new FileOutputStream(uploadFilePath);
			FileInputStream fis=new FileInputStream(upload);
			byte[] buffer=new byte[1024];
			int len=0;
			while((len=fis.read(buffer))!=-1){
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public int getBookId() {
		return this.bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getBookAuthor() {
		return bookAuthor;
	}
	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}
	public String getBookPublish() {
		return bookPublish;
	}
	public void setBookPublish(String bookPublish) {
		this.bookPublish = bookPublish;
	}
	public String getBookISBN() {
		return bookISBN;
	}
	public void setBookISBN(String bookISBN) {
		this.bookISBN = bookISBN;
	}
	public double getBookPrice() {
		return bookPrice;
	}
	public void setBookPrice(double bookPrice) {
		this.bookPrice = bookPrice;
	}
	public int getBookCurrCount() {
		return bookCurrCount;
	}
	public void setBookCurrCount(int bookCurrCount) {
		this.bookCurrCount = bookCurrCount;
	}
	public String getBookPicture() {
		return bookPicture;
	}
	public void setBookPicture(String bookPicture) {
		this.bookPicture = bookPicture;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
}
